# coding:utf-8

import pandas as pd
import jieba
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
import wordcloud as wc

data = pd.read_csv('myhexun.csv')
# 词云图绘制
namelist = data['name'].values.tolist()
name = "".join(namelist)
cut_data = jieba.cut(name)
result = ""
for i in cut_data:
    result = result + " " + str(i)
heart = Image.open('timg.jpeg')
heart = np.array(heart)
font = 'simhei.ttf'
wordpic = wc.WordCloud(collocations=False, mask=heart, font_path=font, background_color='white').generate(result)
plt.imshow(wordpic)
plt.show()
# 评论数-点击量图的绘制
plt.scatter(x=data['comment'], y=data['hits'])
plt.xlabel('commets')
plt.ylabel('hits')
plt.title('commets-hits')
plt.show()
#  异常数据的查找
# 通过观察数据我们发现，commets在250以上的为异常值，hits在25000以上的为异常值,将异常值设为none，然后丢弃
# 首先保存异常值
comment_singular=data['comment'][data['comment']>250].values
hits_singular=data['hits'][data['hits']>25000].values
print("comments的异常值列表为：",comment_singular)
print("hits的异常值列表为:",hits_singular)
# 将异常值去除掉
data['comment'][data['comment']>250]=None
data['hits'][data['hits']>25000]=None
data.dropna(inplace=True)
# 绘制评论数的直方图
commentmax=data['comment'].max()
commentmin=data['comment'].min()
hitsmax=data['hits'].max()
hitsmin=data['hits'].min()
# 极差
commentra=commentmax-commentmin
hitstra=hitsmax-hitsmin
# 组距
comment_width=commentra/15
hits_width=hitstra/15
# 绘制comment直方图
commentrange=np.arange(commentmin,commentmax,comment_width)
plt.hist(data['comment'],commentrange)
plt.show()
# 通过分析我们可以看出comment是长尾分布 大多数的comment处在0-50之间

# 绘制hist直方图
histrange=np.arange(hitsmin,hitsmax,hits_width)
plt.hist(data['hits'],histrange)
plt.show()

# 通过分析我们可以看出hist是长尾分布  大多数hist处在0-5000之间
